<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="12">
            <a-form-model-item label="操作类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="goldActionType">
              <j-dict-select-tag type="list" disabled v-model="model.goldActionType" dictCode="gold_action_type" placeholder="请选择操作类型" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="操作流水号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sn">
              <a-input v-model="model.sn" placeholder="操作流水号" disabled ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="订单号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="orderCodeNum">
              <a-input v-model="model.orderCodeNum" placeholder="订单号" disabled ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="工单号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="workNum">
              <a-input v-model="model.workNum" placeholder="工单号"  disabled></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="拆分页" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="pn">
              <a-input v-model="model.pn" placeholder="拆分页"  disabled></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="款号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="styleCodeNum">
              <a-input v-model="model.styleCodeNum" placeholder="款号" disabled ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="成色" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="karat">
              <j-search-select-tag v-model="model.karat" dict="gold_karat,gold_karat_name,id" placeholder="请选择成色" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="来料名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="name">
              <a-input v-model="model.name" placeholder="请输入来料名称"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="种类" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="cate">
              <j-dict-select-tag type="list" v-model="model.cate" dictCode="gold_type" placeholder="请选择种类" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="规格" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="style">
              <a-input v-model="model.style" placeholder="请输入规格"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="数量" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="count">
              <a-input-number v-model="model.count" placeholder="请输入数量" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="连石重" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="withStoneWeight">
              <a-input v-model="model.withStoneWeight" placeholder="请输入连石重" @change="calculateGoldWeight" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="石重(ct)" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="pureStoneWeight">
              <a-input-number  v-model="model.pureStoneWeight" placeholder="请输入石重(ct)" @change="calculateGoldWeight" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="净重" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="goldWeight">
              <a-input v-model="model.goldWeight" placeholder="净重" @change="goldWeightChange" readOnly ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="耗率(%)" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="attritionRate">
              <a-input-number :min="0" v-model="model.attritionRate" placeholder="请输入耗率(%)" @change="calculateGiveAttrition" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="不计耗重" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="noAttritionWeight">
              <a-input-number :min="0"  v-model="model.noAttritionWeight" placeholder="请输入不计耗重" @change="calculateGiveAttrition" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="给耗重" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="giveAttrition">
              <a-input v-model="model.giveAttrition" placeholder="给耗重" style="width: 100%" readOnly />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="折足率(%)" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="goldContent">
              <a-input-number :min="1" v-model="model.goldContent" placeholder="请输入折足率(%)" @change="calculateGoldContentWeight" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="折足重" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="goldContentWeight">
              <a-input v-model="model.goldContentWeight" placeholder="折足重" style="width: 100%" readOnly />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="工费" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="procedureUnitPrice">
              <a-input-number :min="0" v-model="model.procedureUnitPrice" placeholder="请输入工费" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="单价(￥)" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="unitPrice">
              <a-input-number :min="0" v-model="model.unitPrice" placeholder="请输入单价(￥)" @change="calculateCostAmount" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="价值金额(￥)" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="costAmount">
              <a-input v-model="model.costAmount" placeholder="价值金额(￥)" style="width: 100%" readOnly />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="实付金额(￥)" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="payAmount">
              <a-input-number :min="0" v-model="model.payAmount" placeholder="请输入实付金额(￥)" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="采购单号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="purchaseOrderNum">
              <j-popup
                v-model="model.purchaseOrderNum"
                field="purchaseOrderNum"
                org-fields="purchase_order_num,from_cus,karat,name,cate,style,count,total_weight,gold_content,stone_weight,unit_price,cost_amount,pay_amount,remark"
                dest-fields="purchaseOrderNum,fromCus,karat,name,cate,style,count,totalWeight,goldContent,stoneWeight,unitPrice,costAmount,payAmount,remark"
                code="gold_purchase_report"
                :multi="false"
                @input="popupCallback"
                disabled
                />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="发出方" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="senderName">
              <a-input v-model="model.senderName" style="width: 100%" disabled />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="发出时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sendTime">
              <j-date placeholder="请选择发出时间" v-model="model.sendTime"  style="width: 100%" disabled />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="接收方" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="receiverName">
              <a-input v-model="model.receiverName" style="width: 100%" disabled />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="接收时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="receiveTime">
              <j-date placeholder="请选择接收时间" v-model="model.receiveTime"  style="width: 100%" disabled />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remark">
              <a-textarea v-model="model.remark" rows="4" placeholder="请输入备注" />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'

  export default {
    name: 'GoldEntryForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
           karat: [
              { required: true, message: '请输入成色!'},
           ],
           withStoneWeight: [
              { required: true, message: '请输入连石重!'},
           ],
        },
        url: {
          add: "/goldEntry/add",
          edit: "/goldEntry/edit",
          queryById: "/goldEntry/queryById"
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      goldWeightChange() {
        this.calculateGiveAttrition()//计算给耗重
        this.calculateGoldContentWeight()//计算折足重
        this.calculateCostAmount()//计算价值金额
      },
      calculateGoldWeight () {
        if(this.model.withStoneWeight !== null && this.model.pureStoneWeight !== null && this.model.withStoneWeight !=='' && this.model.pureStoneWeight !=='') {
          this.model.goldWeight = (this.model.withStoneWeight - this.model.pureStoneWeight*0.2).toFixed(2)
        } else {
          this.model.goldWeight = 0
        }
        this.goldWeightChange()
      },
      calculateGiveAttrition () {
        if(this.model.goldWeight !== null && this.model.noAttritionWeight !== null && this.model.attritionRate !== null
          && this.model.goldWeight !=='' && this.model.noAttritionWeight !== '' && this.model.attritionRate !== '') {
          this.model.giveAttrition = ((this.model.goldWeight - this.model.noAttritionWeight) * this.model.attritionRate / 100).toFixed(2)
        } else {
          this.model.giveAttrition = 0
        }
      },
      calculateGoldContentWeight () {
        if(this.model.goldContent !== null && this.model.goldWeight !== null && this.model.goldContent !=='' && this.model.goldWeight !== '') {
          this.model.goldContentWeight = (this.model.goldContent * this.model.goldWeight / 100).toFixed(2)
        } else {
          this.model.goldContentWeight = 0
        }
      },
      calculateCostAmount () {
        if(this.model.unitPrice !== null && this.model.goldWeight !== null && this.model.unitPrice !== '' && this.model.goldWeight !== '') {
          this.model.costAmount =  (this.model.unitPrice * this.model.goldWeight).toFixed(2)
        } else {
          this.model.costAmount = 0
        }
      },
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }

        })
      },
      popupCallback(value,row){
         this.model = Object.assign(this.model, row);
      },
    }
  }
</script>